package com.nuoniu.sibanyun.controller.system;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.SysCompany;
import com.nuoniu.sibanyun.entity.crm.CrmSecretKey;
import com.nuoniu.sibanyun.entity.dto.SysCompanyDto;
import com.nuoniu.sibanyun.service.ISysCompanyService;
import com.nuoniu.sibanyun.service.ISysOfficeService;
import com.nuoniu.sibanyun.service.ISysUserService;
import com.nuoniu.sibanyun.service.crm.ICrmGoodsOrderService;
import com.nuoniu.sibanyun.service.crm.ICrmSecretKeyService;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 公司表 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-04-23
 */
@RestController
@RequestMapping("/sys/company")
@Api(value="公司信息",tags={"公司基本信息接口","WEB_公司信息相关"})
public class SysCompanyController extends BaseController {

    @Autowired
    private ISysCompanyService companyService;
    @Autowired
    private ICrmSecretKeyService secretKeyService;
    @Autowired
    private ISysOfficeService officeService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ICrmGoodsOrderService orderService;
    @Autowired
    private IErpAccountService erpAccountService;

    /**
     * 获取公司列表
     * @author 小啊刚
     * @date 2020-07-24
     */
    @GetMapping("pageList")
    public HttpResult pageList(Page page, SysCompany company) {
        return HttpResult.ok(companyService.pageList(page, company));
    }

    /**
     * 获取公司信息
     * @author 小啊刚
     * @date 2020-07-21
     */
    @GetMapping("getCompanyInfo")
    public HttpResult getCompanyInfo() {
        UserInfo userInfo = UserThreadLocal.get();
        Map company = companyService.getCompanyInfo(userInfo.getCompanyId());
        return HttpResult.ok(company);
    }

    @ApiOperation(value = "获取建账基本信息")
    @GetMapping("getSysCompany")
    public HttpResult<SysCompanyDto> getSysCompany() {
        UserInfo userInfo = UserThreadLocal.get();
        SysCompanyDto companyDto = companyService.getSysCompany(userInfo.getCompanyId());
        return HttpResult.ok(companyDto);
    }

    /**
     * 保存公司信息
     * @author 小啊刚
     * @date 2020-07-21
     */
    @PostMapping("save")
    @ApiOperation(value = "保存公司基本信息")
    public HttpResult save(@RequestBody SysCompany company) {
        UserInfo userInfo = UserThreadLocal.get();
        company.setId(userInfo.getCompanyId().longValue());
        companyService.updateById(company);
        return HttpResult.ok();
    }

    /**
     * 更换密钥
     * @author 小啊刚
     * @date 2020-07-22
     */
    @PostMapping("updateSecretKey")
    public HttpResult updateSecretKey(@RequestBody SysCompany company) {
        UserInfo userInfo = UserThreadLocal.get();
        if(company == null || StringUtils.isBlank(company.getSecretKey())) {
            throw new NuoNiuException("密钥不能为空");
        }

        CrmSecretKey secretKey = secretKeyService.getSecretKey(company.getSecretKey().trim());
        if(secretKey == null) {
            throw new NuoNiuException("密钥无效");
        }

        SysCompany sysCompany = companyService.getById(userInfo.getCompanyId());
        if(sysCompany.getSecretKey() != null && sysCompany.getSecretKey().equals(company.getSecretKey())) {
            throw new NuoNiuException("密钥正在使用，无需更换");
        }

        if(secretKey.getTimes().equals(secretKey.getUseTimes())) {
            throw new NuoNiuException("密钥无效");
        }

        companyService.updateSecretKey(secretKey);
        return HttpResult.ok();
    }

    /**
     * 修改公司名称
     * @author 小啊刚
     * @date 2020-07-22
     */
    @PostMapping("updateCompanyName")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public HttpResult updateCompanyName(@RequestBody SysCompany sysCompany) {
        UserInfo userInfo = UserThreadLocal.get();
        if(StringUtils.isBlank(sysCompany.getCompanyName())) {
            throw new NuoNiuException("单位名称不能为空");
        }

        SysCompany company = companyService.getByName(sysCompany.getCompanyName());
        if(company != null && !(company.getId().equals(userInfo.getCompanyId()))) {
            throw new NuoNiuException("单位名称已存在");
        }

        company = new SysCompany();
        company.setId(userInfo.getCompanyId().longValue());
        company.setCompanyName(sysCompany.getCompanyName());
        companyService.updateById(company);
        //修改机构公司名称
        officeService.updateCompanyName(company);
        //修改用户公司名称
        userService.updateCompanyName(company);
        //修改订单公司名称
        orderService.updateCompanyName(company);
        //更新 账套名称
        erpAccountService.updateAccountName(company);
        return HttpResult.ok();
    }
}
